package com.homework.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 拼音转换工具类
 * 支持常用科目名称转换为拼音
 */
public class PinyinUtil {

    private static final Map<String, String> SUBJECT_PINYIN_MAP = new HashMap<>();

    static {
        // 初始化常用科目拼音映射
        SUBJECT_PINYIN_MAP.put("语文", "yuwen");
        SUBJECT_PINYIN_MAP.put("数学", "shuxue");
        SUBJECT_PINYIN_MAP.put("英语", "yingyu");
        SUBJECT_PINYIN_MAP.put("物理", "wuli");
        SUBJECT_PINYIN_MAP.put("化学", "huaxue");
        SUBJECT_PINYIN_MAP.put("生物", "shengwu");
        SUBJECT_PINYIN_MAP.put("历史", "lishi");
        SUBJECT_PINYIN_MAP.put("地理", "dili");
        SUBJECT_PINYIN_MAP.put("政治", "zhengzhi");
        SUBJECT_PINYIN_MAP.put("其他", "qita");

        // 可能的其他科目
        SUBJECT_PINYIN_MAP.put("语文课", "yuwen");
        SUBJECT_PINYIN_MAP.put("数学课", "shuxue");
        SUBJECT_PINYIN_MAP.put("英语课", "yingyu");
        SUBJECT_PINYIN_MAP.put("物理课", "wuli");
        SUBJECT_PINYIN_MAP.put("化学课", "huaxue");
        SUBJECT_PINYIN_MAP.put("生物课", "shengwu");
        SUBJECT_PINYIN_MAP.put("历史课", "lishi");
        SUBJECT_PINYIN_MAP.put("地理课", "dili");
        SUBJECT_PINYIN_MAP.put("政治课", "zhengzhi");
        SUBJECT_PINYIN_MAP.put("其他课", "qita");

        // 英文科目名称
        SUBJECT_PINYIN_MAP.put("Chinese", "chinese");
        SUBJECT_PINYIN_MAP.put("Math", "math");
        SUBJECT_PINYIN_MAP.put("Mathematics", "mathematics");
        SUBJECT_PINYIN_MAP.put("English", "english");
        SUBJECT_PINYIN_MAP.put("Physics", "physics");
        SUBJECT_PINYIN_MAP.put("Chemistry", "chemistry");
        SUBJECT_PINYIN_MAP.put("Biology", "biology");
        SUBJECT_PINYIN_MAP.put("History", "history");
        SUBJECT_PINYIN_MAP.put("Geography", "geography");
        SUBJECT_PINYIN_MAP.put("Politics", "politics");
        SUBJECT_PINYIN_MAP.put("Other", "other");
    }

    /**
     * 将科目名称转换为拼音
     * @param subjectName 科目名称
     * @return 拼音名称，如果找不到映射则返回默认值
     */
    public static String convertToPinyin(String subjectName) {
        if (subjectName == null || subjectName.trim().isEmpty()) {
            return "unknown";
        }

        // 移除空格并转换为小写
        String trimmedName = subjectName.trim();

        // 直接查找映射
        String pinyin = SUBJECT_PINYIN_MAP.get(trimmedName);
        if (pinyin != null) {
            return pinyin;
        }

        // 如果找不到，尝试去除"课"字
        if (trimmedName.endsWith("课")) {
            String nameWithoutClass = trimmedName.substring(0, trimmedName.length() - 1);
            pinyin = SUBJECT_PINYIN_MAP.get(nameWithoutClass);
            if (pinyin != null) {
                return pinyin;
            }
        }

        // 如果还找不到，返回简单的拼音转换（小写+移除空格）
        return trimmedName.toLowerCase().replaceAll("[^a-zA-Z0-9]", "");
    }

    /**
     * 验证拼音名称是否合法
     * @param pinyinName 拼音名称
     * @return 是否合法
     */
    public static boolean isValidPinyinName(String pinyinName) {
        if (pinyinName == null || pinyinName.trim().isEmpty()) {
            return false;
        }

        // 只允许字母、数字和下划线
        return pinyinName.matches("^[a-zA-Z0-9_]+$");
    }

    /**
     * 获取所有已知的科目拼音映射
     * @return 科目名称到拼音的映射
     */
    public static Map<String, String> getAllSubjectPinyinMap() {
        return new HashMap<>(SUBJECT_PINYIN_MAP);
    }
}